# -*- coding: utf-8 -*-
import scrapy
from scrapy_redis.spiders import RedisSpider
from mySpider.items import MeiJuttItem
import sys,io
import re
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='gb18030')

class MeijuttSpider(RedisSpider):
    name = 'meijutt'
    allowed_domains = ['meijutt.com']
    # start_urls = ['https://www.meijutt.com/1_______.html']
    redis_key = 'meijutt:start_urls'
    def str_of_lists(self,lists,idx):
        if len(lists) > idx:
            return lists[idx]
        return ''
    def isKey(self,key):
        keys = ['原名：', '别名：', '电视台：', '首播日期：', '小分类：']
        if key in keys:
            return True
        matchObj = re.findall(r'\[(.*)\]', key)
        if len(matchObj) >= 1:
            return True
        return False
    def add_empty_str(self,lists):
        res_list = []
        for i in range(len(lists)):
            next_str = lists[i]
            if self.isKey(next_str):
                res_list.append(next_str)
                next_obj = ''
                if len(lists) > i + 1:
                    next_obj = lists[i + 1]
                if self.isKey(next_obj):
                    res_list.append('')
                else:
                    res_list.append(next_obj)

        return res_list

    def remove_empty_str(self, lists):
        res_list = []
        for next_str in lists:
            if not next_str == ' ':
                res_list.append(next_str)
        return res_list
    def parse(self, response):
        # 获取网站标题
        context = response.xpath('/html/head/title/text()')
        # 提取网站标题
        title = context.extract_first()
        print(title)
        names = response.xpath('//ul[@class="list_20"]//li/a/text()').extract()
        fonts = response.xpath('//ul[@class="list_20"]//li//font/text()').extract()
        fonts = self.add_empty_str(fonts)
        all_fonts = response.xpath('//ul[@class="list_20"]//li/text()').extract()
        all_fonts = self.remove_empty_str(all_fonts)
        # print(names)
        # print('--------------------------------------')
        # print(fonts)
        # print('--------------------------------------')
        # print(all_fonts)
        for i in range(len(names)):
            item = MeiJuttItem()
            item['name'] = names[i]
            item['classify'] = self.str_of_lists(fonts,i * 12 )
            item['updateStatus'] = self.str_of_lists(fonts,i * 12 + 1) + all_fonts[i]
            item['originName'] = self.str_of_lists(fonts,i * 12 + 3)
            item['defName'] = self.str_of_lists(fonts,i * 12 + 5)
            item['diantai'] = self.str_of_lists(fonts,i * 12 + 7)
            item['firstUpdate'] = self.str_of_lists(fonts,i * 12 + 9)
            item['subClassify'] = self.str_of_lists(fonts, i * 12 + 11)
            yield item